package unit;

import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;

import junit.framework.TestCase;

import com.google.code.netcheckin.aprs.Station;
import com.google.code.netcheckin.io.StationDbReader;
import com.google.code.netcheckin.io.StationDbWriter;

public class UStationDbWriter extends TestCase {

	protected TestStationDbWriter tsdw;
	protected StringWriter output;
	protected Station stn;
	
	protected static final String[] DATA = new String[] {
		"KB9HCA",
		"Hoagy Carmichael",
		"yes",
		"no",
		"812 330-9999",
		"hoagyc@indiana.edu",
		"BMG, 3rd & High",
		"217 S. High St",
		"Bloomington",
		"IN",
		"47408",
		"39",
		"17.21",
		"N",
		"86",
		"23.71",
		"W",
		"\\-",
		"out by UPS",
		"4817 N Liberty Dr,",
		"Bloomberg",
		"MS",
		"47401",
		"40",
		"21.20",
		"S",
		"87",
		"13.20",
		"E",
		"/[",
	        "TEST PACKET",
	};

	public UStationDbWriter(String name) {
		super(name);
	}

	public void setUp() throws Exception {
		output = new StringWriter();
		tsdw = new TestStationDbWriter(output);
		// use TestStationDbReader to get a populated
		// Station object
		UStationDbReader.TestStationDbReader dbr = 
		 new UStationDbReader.TestStationDbReader(
				new StringReader("X"));
		dbr.testGetColumnIds(toArrayList(StationDbReader.COL_NAMES));
		stn = dbr.testCreateStation(toArrayList(DATA), 1);
		dbr.close();
	}

	public void tearDown() throws Exception {
		if (tsdw != null) {
			tsdw.close();
			tsdw = null;
		}
		stn = null;
		output = null;
	}

		// Be sure that what you put in is what you get out. 

	public void testAAFillBuffer() {
		final String[] answer = tsdw.testFillBuffer(stn);
		assertNotNull("answer null", answer);
		assertEquals("wrong length", DATA.length, answer.length);
		final int ceil = answer.length;
		for (int idx = 0; idx < ceil; idx ++) {
			String field = "field " + new Integer(idx).toString();
			assertEquals(field, DATA[idx], answer[idx]);
		}
	}

		// Write and test for header line, data line, and quoting

	public void testABWrite() {
	  try {
		tsdw.write(stn);
		tsdw.close();
		tsdw = null;
		String answer = output.toString();
		assertNotNull("answer null", answer);
		assertTrue("answer 0 len", answer.length() > 0);
		assertTrue("no header 1", answer.indexOf(
		 StationDbReader.COL_NAMES[StationDbReader.IDX_SEC_ICON]) > -1);
		assertTrue("no header 2", answer.indexOf(
		 StationDbReader.COL_NAMES[StationDbReader.IDX_CALL_SGN]) > -1);
		assertTrue("no data", answer.indexOf("Hoagy") > -1);
		assertTrue("no quote", answer.indexOf("Dr,\",") > -1);
	  } catch(Exception e) {
		System.out.println(e.getMessage());
		e.printStackTrace();
		fail(e.getMessage());
	  }
	}

	public void testACMissingValues() {
		Station stn = new Station();
		stn.setPrimaryLatitude1(Station.MISSING_INT);
		stn.setPrimaryLatitude2(Station.MISSING_FLOAT);
		// WORK HERE when we decide what to do about chars
		//stn.setPrimaryLatitudeOrientation(Station.MISSING_CHAR);
		stn.setPrimaryLongitude1(Station.MISSING_INT);
		stn.setPrimaryLongitude2(Station.MISSING_FLOAT);
		//stn.setPrimaryLongitudeOrientation(Station.MISSING_CHAR);
		stn.setPrimaryIcon(Station.MISSING_STRING);
		stn.setSecondaryLatitude1(Station.MISSING_INT);
		stn.setSecondaryLatitude2(Station.MISSING_FLOAT);
		// WORK HERE when we decide what to do about chars
		//stn.setSecondaryLatitudeOrientation(Station.MISSING_CHAR);
		stn.setSecondaryLongitude1(Station.MISSING_INT);
		stn.setSecondaryLongitude2(Station.MISSING_FLOAT);
		//stn.setSecondaryLongitudeOrientation(Station.MISSING_CHAR);
		stn.setSecondaryIcon(Station.MISSING_STRING);
		String[] formatted = tsdw.testFillBuffer(stn);
		assertNotNull("formatted null", formatted);
		assertEquals("wrong length", StationDbReader.COL_NAMES.length,
				formatted.length);
		assertEquals("bad prim lat deg", "", formatted[StationDbReader.IDX_PRIM_LAT_DEG]);
		assertEquals("bad prim lat min", "", formatted[StationDbReader.IDX_PRIM_LAT_MIN]);
		//assertEquals("bad prim lat dir", "", formatted[StationDbReader.IDX_PRIM_LAT_DIR]);
		assertEquals("bad prim long deg", "", formatted[StationDbReader.IDX_PRIM_LONG_DEG]);
		assertEquals("bad prim long min", "", formatted[StationDbReader.IDX_PRIM_LONG_MIN]);
		//assertEquals("bad prim long dir", "", formatted[StationDbReader.IDX_PRIM_LONG_DIR]);
		assertEquals("bad prim icon", "", formatted[StationDbReader.IDX_PRIM_ICON]);
		assertEquals("bad sec lat deg", "", formatted[StationDbReader.IDX_SEC_LAT_DEG]);
		assertEquals("bad sec lat min", "", formatted[StationDbReader.IDX_SEC_LAT_MIN]);
		//assertEquals("bad sec lat dir", "", formatted[StationDbReader.IDX_SEC_LAT_DIR]);
		assertEquals("bad sec long deg", "", formatted[StationDbReader.IDX_SEC_LONG_DEG]);
		assertEquals("bad sec long min", "", formatted[StationDbReader.IDX_SEC_LONG_MIN]);
		//assertEquals("bad sec long dir", "", formatted[StationDbReader.IDX_SEC_LONG_DIR]);
		assertEquals("bad prim icon", "", formatted[StationDbReader.IDX_PRIM_ICON]);
	}

	///////////// protected below /////////////////////////////

	protected ArrayList toArrayList(final String[] stringa) {
		final int ceil = stringa.length;
		ArrayList answer = new ArrayList(ceil);
		for (int idx = 0; idx < ceil; idx ++) {
			answer.add(stringa[idx]);
		}
		return answer;
	}

		/** Expose protected methods and fields. */

	public class TestStationDbWriter extends StationDbWriter {
		public TestStationDbWriter(Writer w) throws Exception {
			super(w);
		}

		public String[] testFillBuffer(Station stn) {
			fillBuffer(stn);
			return buffers;
		}

	}

}
